// 封装购物车模块
import { defineStore } from "pinia";
import { computed, ref } from "vue";
import { useUserStore } from "@/stores/user.js";
import { insertcartapi, getcartapi, delcartapi } from "@/apis/layout.js";

export const useCartStore = defineStore(
  "cart",
  () => {
    const userStore = useUserStore();
    const isLogin = computed(() => {
      return userStore.loginList.token;
    });
    // 1. 定义state - cartList
    const cartList = ref([]);
    // 2. 定义action - addCart
    const addCart = async (goods) => {
      const { skuId, count } = goods;
      if (isLogin.value) {
        await insertcartapi(skuId, count);
        const res = await getcartapi();
        cartList.value = res.data.result;
      } else {
        const item = cartList.value.find((item) => goods.skuId === item.skuId);
        if (item) {
          // 找到了
          item.count++;
        } else {
          // 没找到
          cartList.value.push(goods);
        }
      }
    };
    const delCart = async (skuId) => {
      if (isLogin.value) {
        await delcartapi([skuId]);
        const res = await getcartapi();
        cartList.value = res.data.result;
      } else {
        const idx = cartList.value.findIndex((item) => {
          skuId === item.skuId;
        });
        cartList.value.splice(idx, 1);
      }
    };
    const allcount = computed(() => {
      return cartList.value.reduce((prev, cur) => {
        return prev + cur.count;
      }, 0);
    });
    const allprice = computed(() => {
      return cartList.value.reduce((prev, cur) => {
        return prev + cur.count * cur.price;
      }, 0);
    });
    const singlecheckstore = (skuId, selected) => {
      const item1 = cartList.value.find((item) => {
        return item.skuId == skuId;
      });
      item1.selected = selected;
    };
    const isAll = computed(() => {
      return cartList.value.every((item) => {
        return item.selected;
      });
    });
    const allcheck = (selected) => {
      cartList.value.map((item) => {
        return (item.selected = selected);
      });
    };
    const clearallcart = () => {
      cartList.value = [];
    };
    const selectedcount = computed(() => {
      return cartList.value
        .filter((item) => {
          return item.selected == true;
        })
        .reduce((prev, cur) => {
          return prev + cur.count;
        }, 0);
    });
    const selectedprice = computed(() => {
      return cartList.value
        .filter((item) => {
          return item.selected == true;
        })
        .reduce((prev, cur) => {
          return prev + cur.count * cur.price;
        }, 0);
    });
    const updatedcart = async () => {
      const res = await getcartapi();
      cartList.value = res.data.result;
    };

    return {
      cartList,
      allcount,
      allprice,
      isAll,
      selectedcount,
      selectedprice,
      isLogin,
      addCart,
      delCart,
      singlecheckstore,
      allcheck,
      clearallcart,
      updatedcart,
    };
  },
  {
    persist: true,
  }
);
